import { PureComponent } from "react";
import store from "../store";
import { changeNameFn } from "../store/modules/useInfo/actionCreators";
export default class Home extends PureComponent {
  // 初始化数据
  state = {
    person: store.getState().useInfo,
  };
  // 修改名字
  changeName = () => {
    store.dispatch(changeNameFn());
  };
  componentDidMount() {
    // 监听仓库变化
    store.subscribe(() => {
      // 修改最新数据
      this.setState({ person: store.getState().useInfo });
    });
  }

  render() {
    const { person } = this.state;
    return (
      <div>
        Home
        <h1>仓库里面的数据</h1>
        <div>
          name:{person.name} -- {person.age}
        </div>
        <button onClick={this.changeName}>点我修改名字</button>
      </div>
    );
  }
}
